package org.example.ExperimentThree.ClientChart;

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {
    private int port;
    private Set<String> usernames = new HashSet<>();
    private Set<PrintWriter> writers = new HashSet<>();

    public ChatServer(int port) {
        this.port = port;
    }
//聊天服务端连接
    public void start() {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("聊天室运行在端口 " + port);

            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("新的客户连接: " + clientSocket);

                PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
                writers.add(writer);

                Thread clientThread = new Thread(new ClientHandler(clientSocket, writer));
                clientThread.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//客户端运行
    private class ClientHandler implements Runnable {
        private Socket clientSocket;
        private BufferedReader reader;
        private PrintWriter writer;

        public ClientHandler(Socket clientSocket, PrintWriter writer) {
            this.clientSocket = clientSocket;
            this.writer = writer;

            try {
                reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            String username = "";
            try {
                writer.println("您的用户名：");
                username = reader.readLine();
                if (username != null) {
                    usernames.add(username);
                    broadcastMessage(username + " 【------------------加入聊天室内-------------】");
                }

                String message;
                while ((message = reader.readLine()) != null) {
                    if (message.equalsIgnoreCase("quit")) {
                        break;
                    }
                    broadcastMessage(username + ": " + message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (username != null) {
                    usernames.remove(username);
                    broadcastMessage(username + " 【----------------离开了聊天室------------】");
                }
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
//服务端广播到各个用户
    private void broadcastMessage(String message) {
        for (PrintWriter writer : writers) {
            writer.println(message);
        }
    }

    public static void main(String[] args) {
        int portNumber = 54239;
        ChatServer server = new ChatServer(portNumber);
        server.start();
    }
}